package my.edu.model.bills.common;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import my.edu.api.LogisticsService;
import my.edu.common.domain.Response;
import my.edu.common.myInterface.GetOne;
import my.edu.constant.BillsType;
import my.edu.constant.OrderNoConstant;
import my.edu.model.bills.purchase.service.IPurchaseOrderService;
import my.edu.model.bills.sales.service.ISalesOrderService;
import my.edu.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.YearMonth;
import java.util.Map;

@RestController
@RequestMapping("/sequence")
@Tag(name = "通用接口")
@Validated
public class CommonController {

    @Autowired
    private OrderNoConstant orderNoConstant;
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private BillsType billsType;
    @Autowired
    private ISalesOrderService salesOrderService;
    @Autowired
    private IPurchaseOrderService purchaseOrderService;

    @GetMapping("/buildNumber")
    @Operation(summary = "生成No")
    public Response buildNumber(String orderType) {
        return switch (orderType) {
            case "purchase" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getPurchaseNo()));
            case "purchaseReturn" ->
                    Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getPurchaseReturnNo()));
            case "sales" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getSalesNo()));
            case "salesReturn" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getSalesReturnNo()));
            case "otherIncome" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getOtherIncomeNo()));
            case "otherExpense" ->
                    Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getOtherExpenseNo()));
            case "assembly" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getAssemblyNo()));
            case "disassembly" -> Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getDisassemblyNo()));
            case "otherOutbound" ->
                    Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getOtherOutboundNo()));
            case "otherInbound" ->
                    Response.success("", OrderNoUtil.generateOrderNo(orderNoConstant.getOtherInboundNo()));
            default -> Response.warn("检查参数");
        };
    }

    @GetMapping("/getLogistics")
    @Operation(summary = "获取物流")
    public Response buildNumber(@RequestParam String logisticsNo,
                                @RequestParam(required = false) String phoneLastFour) {
        JSONObject logistics = logisticsService.getLogistics(logisticsNo, phoneLastFour == null || phoneLastFour.isBlank() ? null : phoneLastFour);

        if (!logistics.getStr("msg").equals("ok") || !logistics.containsKey("list") || logistics.getJSONArray("list").isEmpty()) {
            return Response.warn("未查询到物流信息，或稍后重试");
        } else {
            return Response.success(logistics.getJSONArray("list"));
        }
    }

    @GetMapping("/one")
    @Operation(summary = "单个单据")
    public Response buildNumber(@NotNull(message = "类型不能为空") String type,
                                @NotNull(message = "id不能为空") Long id) {
        Object orderOne = ((GetOne) billsType.getBillsTypeService(type)).getOrderOne(id);
        return Response.success(orderOne);
    }

    @GetMapping("/getBuyAndSaleStatistics")
    @Operation(summary = "获取采购和销售统计信息")
    public Response getBuyAndSaleStatistics() {
        BigDecimal todaySale = salesOrderService.getTodaySale();
        BigDecimal todayBuy = purchaseOrderService.getTodayPurchase();
        BigDecimal monthSale = salesOrderService.getMonthSale();
        BigDecimal monthBuy = purchaseOrderService.getMonthPurchase();
        BigDecimal yesterdaySale = salesOrderService.getYesterdaySale();
        BigDecimal yesterdayBuy = purchaseOrderService.getYesterdayPurchase();
        BigDecimal yearSale = salesOrderService.getYearSale();
        BigDecimal yearBuy = purchaseOrderService.getYearPurchase();
        JSONObject statistics = new JSONObject();
        statistics.put("todaySale", todaySale);
        statistics.put("todayBuy", todayBuy);
        statistics.put("monthSale", monthSale);
        statistics.put("monthBuy", monthBuy);
        statistics.put("yesterdaySale", yesterdaySale);
        statistics.put("yesterdayBuy", yesterdayBuy);
        statistics.put("yearSale", yearSale);
        statistics.put("yearBuy", yearBuy);

        return Response.success(statistics);
    }

    @GetMapping("/buyOrSalePrice")
    @Operation(summary = "获取采购和销售图表信息")
    public Response getBuyOrSalePrice() {
        JSONObject res = new JSONObject();
        JSONArray buy = new JSONArray();
        JSONArray sale = new JSONArray();

        Map<YearMonth, BigDecimal> salePriceList = salesOrderService.getSalePriceList();
        Map<YearMonth, BigDecimal> purchasePriceList =   purchaseOrderService.getPurchasePriceList();

        for (Map.Entry<YearMonth, BigDecimal> yearMonthBigDecimalEntry : purchasePriceList.entrySet()) {
            buy.add(new JSONObject().put("x",yearMonthBigDecimalEntry.getKey().toString()).put("y",yearMonthBigDecimalEntry.getValue()));
        }
        for (Map.Entry<YearMonth, BigDecimal> monthBigDecimalEntry : salePriceList.entrySet()) {
            sale.add(new JSONObject().put("x",monthBigDecimalEntry.getKey().toString()).put("y",monthBigDecimalEntry.getValue()));
        }

        res.putOnce("buyPriceList",buy);
        res.putOnce("salePriceList",sale);

        return Response.success(res);
    }

}
